import {
    CHANGE_CREATOR_FORM,
    CHANGE_CREATOR_FORM_MULTIPART,
    FETCH_COURSES_SUCCESS,
    INIT_CREATOR_FORM,
    POST_COURSE_SUCCESS,
    CLOSE_CREATE_SUCCESS_MODAL
} from '../constants'
import {get, post, url} from '../utils/request'

export const courseListActions = {
    fetchAllCourses: () => {
        return dispatch => {
            return get(url.getEvents).then(res => {
                if (!res.error) {
                    dispatch({
                        type: FETCH_COURSES_SUCCESS,
                        data: res.data
                    })
                } else {
                    console.error("获取数据失败", res.error)
                }
            })

        }
    },
    createCourse: formData => {
        return dispatch => {
            return post(url.createEvent, wrapFormData(formData)).then(res => {
                if (!res.error) {
                    dispatch({
                        type: POST_COURSE_SUCCESS,
                        data: {...res.data}
                    })
                } else {
                    console.error("创建课程失败", res.error)
                }
            })
        }
    },
    initForm: () => ({type: INIT_CREATOR_FORM}),
    changeForm: (field, value) => ({
        type: CHANGE_CREATOR_FORM,
        field: field,
        value: value
    }),
    changeFormMultipart: obj => ({
        type: CHANGE_CREATOR_FORM_MULTIPART,
        obj: obj
    }),
    closeCreateSuccessModal: () =>({type:CLOSE_CREATE_SUCCESS_MODAL})

}
/*
    "title": "1",
    "description": "2",
    "event_agenda": "3",
    "startTime": "09:05",
    "startDate": "2019-06-18",
    "endTime": "17:00",
    "endDate": "2019-06-18",
    "place": "4",
    "address": "5",
    "name": "6",
    "phone": "7",
    "other": "",
    "max_applicants_number": "8"
    "owner": false
 */

const wrapFormData = formData => {
    return {
        ...formData

    }
}


export default courseListActions;
